const router = require('express').Router()
const config = require('../../../public/javascripts/config')
const db = require('../../../public/javascripts/db')

//获取所有串口信息
router.get('/serials', (req, res) => {
    config.getAllSerialPort((ports) => {
        res.status(200).json(ports)
    })
})

//获取配置信息
router.get('/config', (req, res) => {
    // db.getConfigInfo((data) => {
    //     res.status(200).json(data)
    // })
    var info = config.getConfigInfo()
    res.status(200).json(info)
})

//更新配置信息
router.put('/config', (req, res) => {
    var  values  = req.body
    config.updateConfig(values, (err) => {
        if (!err)
            config.updateConfig(values, () => {
                res.status(200).json({ msg: 'ok' })
            })
        else
            res.status(405).json({ msg: err })
    })
})

//更新串口信息
router.put('/config/serialinfo', (req, res) => {
    var values = req.body
    config.updateSerialInfo(values, (flag) => {
        if (flag) res.status(200).json({ msg: "更新成功" })
        else res.status(200).json({ msg: "更新失败" })
    })
})

router.get('/', (req, res) => {
    // var str = '-1.1,+0.2,+0.3,+0.4,+0.5,+0.6,+0.7,+0.8,      OK,ng,ng,ng,ng,ng,ng,ng,-NG,pa101,,2021-07-02,00:14:12'
    // var frame = config.getFrame()
    var str='C481,OK,53.0012,1,3.9949,2,22.9981,B'
    var frame=require('../../../public/javascripts/frame/vat')
    var obj = frame.resolve(str)
    console.log(obj)
    db.insertRecord(obj.data, (flag) => {
        console.log(flag)
    })
    // res.status(200).json(obj.data)
    // var t = require('../../../public/javascripts/db')
    // t.getConfig().then((test) => {
    //     console.log(test)
    //     res.status(200).json(test)
    // })
    // var test=JSON.stringify(t.dataValues)
    res.status(200).json({ msg: "test" })
})

router.post('/', (req, res) => {
    db.deleteLastRecord((flag) => {
        console.log(flag)
    })
    res.status(200).json({ msg: "test" })
})

module.exports = router